package com.linkage.jz.bankruptcy.adapter.Vote;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.expand.adapter.BaseExpandableAdapter;
import com.linkage.jz.bankruptcy.adapter.expand.model.ExpandableListItem;
import com.linkage.jz.bankruptcy.adapter.expand.viewholder.AbstractAdapterItem;
import com.linkage.jz.bankruptcy.adapter.expand.viewholder.AbstractExpandableAdapterItem;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVotersDetail;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVotersDetailGroup;
import com.linkage.seapeak.utils.CommonUtils;

import java.text.DecimalFormat;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 表决人员明细--带分组的adapter
 */
public class ProVoteVotersDetailGroupsAdapter extends BaseExpandableAdapter {

    public static final int ITEM_TYPE_GROUP = 1000;
    public static final int ITEM_TYPE_PERSON = 1001;
    private Context context;
    private ParentReqChildListener itemClickListener;

    private DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.

    public ProVoteVotersDetailGroupsAdapter(Context context, List<ProVoteVotersDetailGroup> data) {
        super(data);
        this.context = context;
    }

    @Override
    public Object getItemViewType(Object t) {
        if (t instanceof ProVoteVotersDetailGroup) {
            return ITEM_TYPE_GROUP;
        } else if (t instanceof ProVoteVotersDetail)
            return ITEM_TYPE_PERSON;
        return -1;
    }

    @NonNull
    @Override
    public AbstractAdapterItem<Object> getItemView(Object type) {
        int itemType = (int) type;
        switch (itemType) {
            case ITEM_TYPE_GROUP:
                return new GroupItem();
            case ITEM_TYPE_PERSON:
                return new PersonItem();
        }
        return null;
    }

    class GroupItem extends AbstractExpandableAdapterItem implements View.OnClickListener {
        LinearLayout rootItemView;
        TextView tvName;
        ImageView expandIv;

        @Override
        public int getLayoutResId() {
            return R.layout.adapter_pro_vote_voters_detail_group;
        }

        @Override
        public void onBindViews(View root) {
            rootItemView = root.findViewById(R.id.item_root);
            tvName = root.findViewById(R.id.item_name);
            expandIv = root.findViewById(R.id.item_arrow);
            rootItemView.setOnClickListener(this);
        }

        @Override
        public void onExpansionToggled(boolean expanded) {
            if (expanded) {
                expandIv.animate().setDuration(150).rotation(90);
            } else {
                expandIv.animate().setDuration(150).rotation(0);
            }
        }

        @Override
        public void onSetViews() {
            //expandIv.setVisibility(View.GONE);
        }

        @Override
        public void onUpdateViews(Object model, int position) {
            super.onUpdateViews(model, position);
            onSetViews();
            ProVoteVotersDetailGroup group = (ProVoteVotersDetailGroup) model;
            tvName.setText(group.getName() + "(" + group.getMemberCount() + ")");
            ExpandableListItem parentListItem = (ExpandableListItem) model;
            List<?> childItemList = parentListItem.getChildItemList();
            if (childItemList != null && !childItemList.isEmpty()) {
                //mArrow.setVisibility(View.VISIBLE);
                //mExpand.setText(parentListItem.isExpanded() ? "unexpand" : "expand");
            } else {
                //mExpand.setText("expand");
            }
        }

        @Override
        public void onClick(View view) {
            /**
             * control item expand and unexpand
             */
//            if (getExpandableListItem() != null && getExpandableListItem().getChildItemList() != null) {
            ProVoteVotersDetailGroup pg = (ProVoteVotersDetailGroup)getExpandableListItem();
            if(pg.isRequestChild()) {
                if (getExpandableListItem().isExpanded()) {
                    collapseView();
                } else {
                    expandView();
                }
            }else {
                itemClickListener.onReq(pg);
            }
        }
    }

    public void expandItem(ProVoteVotersDetailGroup item) {
        int index = -1;
        for(int i=0;i<mDataList.size();i++) {
            Object obj = mDataList.get(i);
            if(obj instanceof ProVoteVotersDetailGroup) {
                ProVoteVotersDetailGroup pg = (ProVoteVotersDetailGroup)obj;
                if (item.getId().equals(pg.getId())) {
                    index = i;
                    break;
                }
            }
        }
        if(index > -1) {
            expandParent(index);
        }
    }

    public void setRequestChild(ParentReqChildListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public static abstract class ParentReqChildListener {
        public abstract void onReq(ProVoteVotersDetailGroup pg);
    }

    class PersonItem extends AbstractAdapterItem {

        private ImageView avatar;
        private TextView name;
        private TextView sureMoney;
        private TextView voteMoney;
        private TextView changeBtn;
        private TextView voteCode;
//        private TextView online;

        @Override
        public int getLayoutResId() {
            return R.layout.adapter_pro_vote_voters_detail_no_group;
        }

        @Override
        public void onBindViews(View root) {
            avatar = root.findViewById(R.id.voter_avatar);
            name = root.findViewById(R.id.voter_name);
            sureMoney = root.findViewById(R.id.voter_sure_money);
            voteMoney = root.findViewById(R.id.voter_vote_money);
            changeBtn = root.findViewById(R.id.voter_change);
            voteCode = root.findViewById(R.id.voter_code);
//            online = root.findViewById(R.id.online);
        }

        @Override
        public void onSetViews() {

        }

        @Override
        public void onUpdateViews(Object model, int position) {
            if (model instanceof ProVoteVotersDetail) {
                ProVoteVotersDetail item = (ProVoteVotersDetail) model;
//                avatar.setImageResource(R.drawable.logo);
                name.setText(item.getCreditorName());
//                Glide.with(context).load(item.getAvatar())
//                        .bitmapTransform(new CropCircleTransformation(context))
//                        .placeholder(R.drawable.logo)
//                        .into(avatar);

                ImageView imageview = avatar;

                Glide.with(context).load(item.getAvatar())
                        .bitmapTransform(new CropCircleTransformation(context))
                        .placeholder(R.drawable.logo)
                        .into( imageview);


//                if (item.getOnlineStatus()==2) {
//                    online.setVisibility(View.VISIBLE);
//                } else {
//                    online.setVisibility(View.INVISIBLE);
//                }

                sureMoney.setText("确认金额：" + CommonUtils.resetMoney(item.getConfirmedMoney()) + "元");
                voteMoney.setText("表决金额：" +CommonUtils.resetMoney(item.getVoteMoney()) + "元");
                if(TextUtils.isEmpty(item.getMeetingNumber())) {
                    voteCode.setVisibility(View.GONE);
                }else {
                    voteCode.setVisibility(View.VISIBLE);
                    voteCode.setText("参会编号：" + item.getMeetingNumber());
                }
            }
        }
    }
}
